/* eslint-disable consistent-return */
/* eslint-disable array-callback-return */

import React, { Component } from 'react';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { Card, Table, Button, Modal, Spin, Badge, Popover, Steps, Divider, Timeline } from 'antd';
import { connect } from 'dva';
import router from 'umi/router';
import moment from 'moment';
import FooterToolbar from '@/components/FooterToolbar';
import DescriptionList from '@/components/DescriptionList';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import ProRecording from '@/pages/components/Problem/ProblemPage/ProRecording';
import HdlRemark from '@/pages/components/HdlRemark';

const { Description } = DescriptionList;
const { Step } = Steps;

@connect(({ addempsend, loading, addemprec, reduceempsend }) => ({
  addempsend,
  detailsData: reduceempsend.detailsData,
  loading: loading.models.addempsend,
  payDetailsData: addemprec.payDetailsData,
  execStatusDto: addempsend.detailsData.execStatusDto || [], //  执行状态
}))
@Form.create()
class Problem extends Component {
  constructor(props) {
    super(props);
    this.state = {
      maistrue: false,
      loading: false,
      stepDirection: 'horizontal',
      activeKey: '1',
    };
  }

  componentDidMount() {
    this.getDetail();
  }

  // 详情
  getDetail = () => {
    this.setState({
      loading: true,
    });
    const { dispatch, location } = this.props;
    const { sendOrdCutTaskId, activeKey } = location.query;
    dispatch({
      type: 'reduceempsend/feactorderDetail',
      payload: { sendOrdCutTaskId },
      callback: () => {
        this.setState({
          loading: false,
        });
      },
    });
    this.setState({ activeKey });
  };

  // 缴纳明细弹框
  detailed = () => {
    const { dispatch, location } = this.props;
    const { recvOrdAddTaskId } = location.query;
    dispatch({
      type: 'addemprec/getPaymentDetails',
      payload: { recvOrdAddTaskId },
    });

    this.setState({
      maistrue: true,
    });
  };

  // 关闭缴纳明细弹框
  handleOk = () => {
    this.setState({
      maistrue: false,
    });
  };

  // 材料信息（来自员工系统）
  getVal = value => {
    if (value.inpVal) {
      if (value.inpType && value.inpType === 9 && value.inpVal !== '[]') {
        return this.addLink(value.inpVal);
      }
      if (value.inpType && value.inpType === 9 && value.inpVal === '[]') {
        return '--';
      }
      return this.commanName(value);
    }
    return '--';
  };

  render() {
    const { detailsData, payDetailsData } = this.props;
    const { loading, stepDirection, activeKey, maistrue } = this.state;
    const newProtInfoDto = [];
    if (detailsData.protInfoDto) {
      newProtInfoDto.push(detailsData.protInfoDto);
    }
    const newordCutPdList = [];
    if (detailsData.pdInfoDtos) {
      detailsData.pdInfoDtos.map(pditem => {
        newordCutPdList.push(pditem);
      });
    }
    if (detailsData.serSetInfoDto && detailsData.serSetInfoDto.detailDto) {
      detailsData.serSetInfoDto.detailDto.map(item => {
        const temp = item;
        temp.pdName = temp.assName;
        temp.cityName = temp.cityName;
        temp.ass = temp.ass;
        newordCutPdList.splice(1, 0, temp);
      });
    }
    // 减少产品
    const columns = [
      {
        title: '产品',
        dataIndex: 'pdName',
        onHeaderCell: () => ({ style: { fontWeight: 'bold' } }),
      },
      {
        title: '执行地',
        dataIndex: 'cityName',
        onHeaderCell: () => ({ style: { fontWeight: 'bold' } }),
      },
      {
        title: '业务结束日期',
        dataIndex: 'endDate',
        onHeaderCell: () => ({ style: { fontWeight: 'bold' } }),
        render: val => {
          if (val && val && moment(val).format('YYYY-MM-01')) {
            const start = val && moment(val).format('YYYY-MM-01');
            const end = moment(start)
              .add('month', 1)
              .add('days', -1)
              .format('YYYY-MM-DD');
            return end;
          }
        },
      },
      {
        title: '回单时间',
        dataIndex: 'receTime',
        onHeaderCell: () => ({
          style: {
            fontWeight: 'bold',
            display: activeKey === '3' || activeKey === '4' ? 'table-cell' : 'none',
          },
        }),
        onCell: () => ({
          style: {
            display: activeKey === '3' || activeKey === '4' ? 'table-cell' : 'none',
          },
        }),
        render: val => (val && moment(val).format('YYYY-MM-DD HH:mm')) || '--',
      },
      {
        title: '修改说明',
        dataIndex: 'editDesc',
        onHeaderCell: () => ({
          style: {
            fontWeight: 'bold',
            display: activeKey === '3' || activeKey === '4' ? 'table-cell' : 'none',
          },
        }),
        onCell: () => ({
          style: {
            display: activeKey === '3' || activeKey === '4' ? 'table-cell' : 'none',
          },
        }),
        render: val => val || '--',
      },
    ];
    // 社保公积金明细
    const insAccuColumns = [
      {
        title: '产品名称',
        dataIndex: 'assName',
        align: 'center',
        onHeaderCell: () => ({ style: { fontWeight: 'bold' } }),
      },
      {
        title: '业务开始日期',
        dataIndex: 'beginDate',
        align: 'center',
        onHeaderCell: () => ({ style: { fontWeight: 'bold' } }),
        render: beginDate => (beginDate && moment(beginDate).format('YYYY-MM-01')) || '--',
      },
      {
        title: '业务结束日期',
        dataIndex: 'endDate',
        align: 'center',
        sorter: (a, b) => new Date(b.endDate) - new Date(a.endDate),
        onHeaderCell: () => ({ style: { fontWeight: 'bold' } }),
        render: val => {
          if (val && val && moment(val).format('YYYY-MM-01')) {
            const start = val && moment(val).format('YYYY-MM-01');
            const end = moment(start)
              .add('month', 1)
              .add('days', -1)
              .format('YYYY-MM-DD');
            return end;
          }
        },
      },
      {
        title: '缴纳金额',
        dataIndex: 'payAmount',
        onHeaderCell: () => ({ style: { fontWeight: 'bold', textAlign: 'center' } }),
        onCell: () => ({ style: { textAlign: 'right' } }),
        render: val => val || (val === 0 ? 0 : null),
      },
      {
        title: '企业基数',
        dataIndex: 'entpBase',
        onHeaderCell: () => ({ style: { fontWeight: 'bold', textAlign: 'center' } }),
        onCell: () => ({ style: { textAlign: 'right' } }),
        render: val => val || (val === 0 ? 0 : null),
      },
      {
        title: '企业比例(%)',
        dataIndex: 'entpProp',
        align: 'center',
        onHeaderCell: () => ({ style: { fontWeight: 'bold' } }),
        render: val => val || (val === 0 ? 0 : null),
      },
      {
        title: '企业附加(元)',
        dataIndex: 'entpAddnAmo',
        onHeaderCell: () => ({ style: { fontWeight: 'bold', textAlign: 'center' } }),
        onCell: () => ({ style: { textAlign: 'right' } }),
        render: val => val || (val === 0 ? 0 : null),
      },
      {
        title: '企业缴费',
        dataIndex: 'entpPay',
        onHeaderCell: () => ({ style: { fontWeight: 'bold', textAlign: 'center' } }),
        onCell: () => ({ style: { textAlign: 'right' } }),
        render: val => val || (val === 0 ? 0 : null),
      },
      {
        title: '个人基数',
        dataIndex: 'perBase',
        onHeaderCell: () => ({ style: { fontWeight: 'bold', textAlign: 'center' } }),
        onCell: () => ({ style: { textAlign: 'right' } }),
        render: val => val || (val === 0 ? 0 : null),
      },
      {
        title: '个人比例(%)',
        dataIndex: 'perProp',
        align: 'center',
        onHeaderCell: () => ({ style: { fontWeight: 'bold' } }),
      },
      {
        title: '个人附加(元)',
        dataIndex: 'perAddnAmo',
        onHeaderCell: () => ({ style: { fontWeight: 'bold', textAlign: 'center' } }),
        onCell: () => ({ style: { textAlign: 'right' } }),
        render: val => val || (val === 0 ? 0 : null),
      },
      {
        title: '个人缴费',
        dataIndex: 'perPay',
        onHeaderCell: () => ({ style: { fontWeight: 'bold', textAlign: 'center' } }),
        onCell: () => ({ style: { textAlign: 'right' } }),
        render: val => val || (val === 0 ? 0 : null),
      },
      {
        title: '执行地区',
        dataIndex: 'cityName',
        align: 'center',
        onHeaderCell: () => ({ style: { fontWeight: 'bold' } }),
      },
    ];
    // 减少险种
    const addassColumns = [
      {
        title: '产品',
        dataIndex: 'assName',
        onHeaderCell: () => ({ style: { fontWeight: 'bold' } }),
      },
      {
        title: '执行地',
        dataIndex: 'areaName',
        onHeaderCell: () => ({ style: { fontWeight: 'bold' } }),
      },
      {
        title: '业务结束日期',
        dataIndex: 'endDate',
        onHeaderCell: () => ({ style: { fontWeight: 'bold' } }),
        render: val => {
          if (val && val && moment(val).format('YYYY-MM-01')) {
            const start = val && moment(val).format('YYYY-MM-01');
            const end = moment(start)
              .add('month', 1)
              .add('days', -1)
              .format('YYYY-MM-DD');
            return end;
          }
        },
      },
      {
        title: '回单时间',
        dataIndex: 'receTime',
        onHeaderCell: () => ({
          style: {
            fontWeight: 'bold',
            display: activeKey === '3' || activeKey === '4' ? 'table-cell' : 'none',
          },
        }),
        onCell: () => ({
          style: {
            display: activeKey === '3' || activeKey === '4' ? 'table-cell' : 'none',
          },
        }),
        render: val => (val && moment(val).format('YYYY-MM-DD HH:mm')) || '--',
      },
      {
        title: '修改说明',
        dataIndex: 'editDesc',
        onHeaderCell: () => ({
          style: {
            fontWeight: 'bold',
            display: activeKey === '3' || activeKey === '4' ? 'table-cell' : 'none',
          },
        }),
        onCell: () => ({
          style: {
            display: activeKey === '3' || activeKey === '4' ? 'table-cell' : 'none',
          },
        }),
        render: val => val || '--',
      },
    ];
    const customDot = (dot, { status }) =>
      status === 'process' ? (
        <Popover placement="topLeft" arrowPointAtCenter>
          {dot}
        </Popover>
      ) : (
        dot
      );
    // 判断是否为空对象
    const isEmptyObj = obj => {
      if (obj) {
        return JSON.stringify(obj) === '{}';
      }
      return false;
    };
    const status1 = (item, leafDet) => {
      if (item.operLeafType === 4) {
        return leafDet.doneFlag === 3 ? 'success' : 'default';
      }
      return leafDet.doneFlag === 1 ? 'success' : 'default';
    };

    return (
      <PageHeaderWrapper>
        <Spin tip="Loading..." spinning={loading}>
          {/* 员工信息 */}
          {detailsData.responseSendOrdDetailDto && (
            <Card title={<span>员工信息</span>}>
              {(!isEmptyObj(detailsData.responseSendOrdDetailDto) && (
                <Form>
                  <DescriptionList>
                    <Description term="姓名">
                      {detailsData.responseSendOrdDetailDto.empName || '--'}
                    </Description>
                    <Description term="证件类型">
                      {detailsData.responseSendOrdDetailDto.idTypeName || '--'}
                    </Description>
                    <Description term="证件号">
                      {detailsData.responseSendOrdDetailDto.idCode || '--'}
                    </Description>
                    <Description term="国籍">
                      {detailsData.responseSendOrdDetailDto.nationName || '--'}
                    </Description>
                    <Description term="出生日期">
                      {detailsData.responseSendOrdDetailDto
                        ? moment(detailsData.responseSendOrdDetailDto.birthDate).format(
                            'YYYY-MM-DD'
                          )
                        : '--'}
                    </Description>
                    <Description term="性别">
                      {detailsData.responseSendOrdDetailDto.genderName || '--'}
                    </Description>
                    <Description term="电话">
                      {detailsData.responseSendOrdDetailDto.mobile || '--'}
                    </Description>
                    <Description term="员工邮箱">
                      {detailsData.responseSendOrdDetailDto.email || '--'}
                    </Description>
                  </DescriptionList>
                  <Divider dashed />
                  <DescriptionList>
                    <Description term="客户名称">
                      {detailsData.responseSendOrdDetailDto.custName || '--'}
                    </Description>
                    <Description term="商务合同">
                      {detailsData.responseSendOrdDetailDto.custContName || '--'}
                    </Description>
                    {!detailsData.assDetailDtoList &&
                      detailsData.responseSendOrdDetailDto &&
                      detailsData.responseSendOrdDetailDto.evaDate && (
                        <Description term="离职日期">
                          {detailsData.responseSendOrdDetailDto.evaDate
                            ? moment(detailsData.responseSendOrdDetailDto.evaDate).format(
                                'YYYY-MM-DD'
                              )
                            : '--'}
                        </Description>
                      )}
                    {detailsData.responseSendOrdDetailDto &&
                      detailsData.responseSendOrdDetailDto.deduConFlagName && (
                        <Description term="是否离职联系">
                          {detailsData.responseSendOrdDetailDto.deduConFlagName || '--'}
                        </Description>
                      )}
                    <Description term="社保停做原因">
                      {detailsData.responseSendOrdDetailDto.evaReasonName || '--'}
                    </Description>
                    {!detailsData.assDetailDtoList &&
                      detailsData.responseSendOrdDetailDto &&
                      detailsData.responseSendOrdDetailDto.unempFlagName && (
                        <Description term="是否办理失业">
                          {detailsData.responseSendOrdDetailDto.unempFlagName || '--'}
                        </Description>
                      )}
                    <Description term="接单方">
                      {detailsData.responseSendOrdDetailDto.recvSuppName || '--'}
                    </Description>
                    <Description term="业务部">
                      {detailsData.responseSendOrdDetailDto.sendDepName || '--'}
                    </Description>
                    <Description term="业务员">
                      {detailsData.responseSendOrdDetailDto.sendRespName || '--'}
                    </Description>
                    {detailsData.responseSendOrdDetailDto.hairOrdDate && (
                      <Description term="发单时间">
                        {(detailsData.responseSendOrdDetailDto.hairOrdDate &&
                          moment(detailsData.responseSendOrdDetailDto.hairOrdDate).format(
                            'YYYY-MM-DD HH:mm'
                          )) ||
                          '--'}
                      </Description>
                    )}
                  </DescriptionList>
                </Form>
              )) ||
                '暂无数据'}
            </Card>
          )}
          {/* 减少产品 */}
          {detailsData.pdInfoDtos && (
            <Card title={<span>减少产品</span>}>
              {(detailsData.pdInfoDtos.length && (
                <Table
                  columns={columns}
                  dataSource={newordCutPdList}
                  pagination={false}
                  style={{ maxWidth: 800 }}
                />
              )) ||
                '暂无数据'}
            </Card>
          )}
          {/* 减少险种 */}
          {detailsData.assDetailDtoList && (
            <Card title={<span>减少险种</span>}>
              {(detailsData.assDetailDtoList.length && (
                <Table
                  dataSource={detailsData.assDetailDtoList || []}
                  columns={addassColumns}
                  rowKey={(_, index) => index}
                  pagination={false}
                />
              )) ||
                '暂无数据'}
            </Card>
          )}
          {/* 执行状态 */}
          {detailsData.execStatusDto && (
            <Card title={<span>执行状态</span>}>
              {detailsData.execStatusDto.leafList && detailsData.execStatusDto.leafList.length ? (
                <Steps
                  direction={stepDirection}
                  progressDot={customDot}
                  style={{ width: '60%', margin: '0 auto', marginTop: 20 }}
                >
                  {detailsData.execStatusDto &&
                    detailsData.execStatusDto.leafList.map(item => (
                      <Step
                        key={item.operLeafTypeName}
                        title={item.operLeafTypeName}
                        description={
                          !item.leafDetList.length ? (
                            `无${item.operLeafType === 3 ? '办理' : '申报'}内容`
                          ) : (
                            <div style={{ width: 400, fontSize: 12, textAlign: 'left' }}>
                              {item.leafDetList.map(leafDet => (
                                <div>
                                  <Badge
                                    status={status1(item, leafDet)}
                                    text={
                                      <span style={{ lineHeight: '30px' }}>
                                        {leafDet.leafDetTypeName} :{leafDet.doneFlagName || '--'}
                                        <span
                                          style={{
                                            fontSize: '12px',
                                            color: '#999',
                                            marginLeft: 15,
                                          }}
                                        >
                                          {leafDet.operTime &&
                                            moment(leafDet.operTime).format('YYYY-MM-DD')}
                                        </span>
                                      </span>
                                    }
                                  />
                                  <br />
                                </div>
                              ))}
                            </div>
                          )
                        }
                        status={item.doneFlag === 1 ? 'finish' : 'wait'}
                      />
                    ))}
                </Steps>
              ) : (
                '暂无数据'
              )}
            </Card>
          )}
          {/* 材料信息 */}
          {detailsData.dataDtos && (
            <Card title={<span>材料信息</span>} bordered={false}>
              {(detailsData.dataDtos.length && (
                <DescriptionList>
                  {detailsData.dataDtos.map((item, index) => (
                    // eslint-disable-next-line react/no-array-index-key
                    <Description key={index} term={item.matName}>
                      {this.getVal(item)}
                    </Description>
                  ))}
                </DescriptionList>
              )) ||
                '暂无数据'}
            </Card>
          )}

          {/* 办理备注 */}
          {detailsData.handDto && <HdlRemark hdlRemark={detailsData.handDto} />}

          {/* 问题记录 */}
          {detailsData.probDets && <ProRecording proRecording={detailsData.probDets} />}

          <FooterToolbar style={{ width: '100%' }}>
            <Button onClick={() => router.goBack()}>返回</Button>
          </FooterToolbar>
          <Modal
            width="1200px"
            visible={maistrue}
            onCancel={this.handleOk}
            title="社保公积金明细"
            footer={[
              <Button key="submit" type="primary" onClick={this.handleOk}>
                关闭
              </Button>,
            ]}
          >
            <Card>
              <Table
                dataSource={payDetailsData}
                columns={insAccuColumns}
                rowKey={record => record.serSetId}
                pagination={false}
              />
            </Card>
          </Modal>
        </Spin>
      </PageHeaderWrapper>
    );
  }
}

export default Problem;
